﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WinUI {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e) {
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK) {
                StringBuilder sb = new StringBuilder();

                // Read the files
                foreach (string file in openFileDialog1.FileNames) {
                    sb.Append(file);
                    sb.AppendLine();
                }

                if (sb.Length > 0) {
                    txtXml.Text = sb.ToString();
                }
            }
        }

        private void btnOutputFolder_Click(object sender, EventArgs e) {
            DialogResult dr = folderBrowserDialog1.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK) {
                txtOutDir.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void btnGenAll_Click(object sender, EventArgs e) {
            if (txtXml.Text.Trim() != string.Empty) {
                string[] fileList = txtXml.Text.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string file in fileList) {
                    PostgreSqlTable table = new PostgreSqlTable(file);

                    try {
                        string modelJava = PostgreSqlJavaGenerator.GenModelJava(table);
                        string modelFileName = string.Format(@"{0}\{1}Model.java", txtOutDir.Text.Trim(), PostgreSqlJavaGenerator.GenJavaName(table.Name));
                        File.WriteAllText(modelFileName, modelJava);

                        string modelExtJava = PostgreSqlJavaGenerator.GenInputJava(table);
                        string modelExtFileName = string.Format(@"{0}\{1}ModelExt.java", txtOutDir.Text.Trim(), PostgreSqlJavaGenerator.GenJavaName(table.Name));
                        File.WriteAllText(modelExtFileName, modelExtJava);

                        string servletJava = PostgreSqlJavaGenerator.GenServletJava(table);
                        string servletFileName = string.Format(@"{0}\{1}Servlet.java", txtOutDir.Text.Trim(), PostgreSqlJavaGenerator.GenJavaName(table.Name));
                        File.WriteAllText(servletFileName, servletJava);

                        string webServiceJava = PostgreSqlJavaGenerator.GenWebServiceJava(table);
                        string webServiceFileName = string.Format(@"{0}\{1}WS.java", txtOutDir.Text.Trim(), PostgreSqlJavaGenerator.GenJavaName(table.Name));
                        File.WriteAllText(webServiceFileName, webServiceJava);

                        string newJSP = PostgresqlJspGenerator.GenNewJSP(table);
                        string newJSPFileName = string.Format(@"{0}\{1}New.jsp", txtOutDir.Text.Trim(), table.JavaNameLowerCase);
                        File.WriteAllText(newJSPFileName, newJSP);

                        string editJSP = PostgresqlJspGenerator.GenEditJSP(table);
                        string editJSPFileName = string.Format(@"{0}\{1}Edit.jsp", txtOutDir.Text.Trim(), table.JavaNameLowerCase);
                        File.WriteAllText(editJSPFileName, editJSP);

                        string inputJSP = PostgresqlJspGenerator.GenInputJSP(table);
                        string inputJSPFileName = string.Format(@"{0}\{1}Input.jsp", txtOutDir.Text.Trim(), table.JavaNameLowerCase);
                        File.WriteAllText(inputJSPFileName, inputJSP);

                        string viewJSP = PostgresqlJspGenerator.GenViewJSP(table);
                        string viewJSPFileName = string.Format(@"{0}\{1}View.jsp", txtOutDir.Text.Trim(), table.JavaNameLowerCase);
                        File.WriteAllText(viewJSPFileName, viewJSP);

                        string listJSP = PostgresqlJspGenerator.GenListJSP(table);
                        string listJSPFileName = string.Format(@"{0}\{1}List.jsp", txtOutDir.Text.Trim(), table.JavaNameLowerCase);
                        File.WriteAllText(listJSPFileName, listJSP);

                    } catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                }

                MessageBox.Show("All files generated");
            }
        }

        private void btnGenModel_Click(object sender, EventArgs e) {
            if (txtXml.Text.Trim() != string.Empty) {
                string[] fileList = txtXml.Text.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string file in fileList) {
                    try {
                        PostgreSqlTable table = new PostgreSqlTable(file);

                        try {
                            string modelJava = PostgreSqlJavaGenerator.GenModelJava(table);
                            string modelFileName = string.Format(@"{0}\{1}Model.java", txtOutDir.Text.Trim(), PostgreSqlJavaGenerator.GenJavaName(table.Name));
                            File.WriteAllText(modelFileName, modelJava);
                        } catch (Exception ex) {
                            MessageBox.Show(ex.ToString());
                        }
                    } catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                }

                MessageBox.Show("Model files generated");
            }
        }

        private void btnGenModelExt_Click(object sender, EventArgs e) {
            if (txtXml.Text.Trim() != string.Empty) {
                string[] fileList = txtXml.Text.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string file in fileList) {
                    PostgreSqlTable table = new PostgreSqlTable(file);

                    try {
                        string modelExtJava = PostgreSqlJavaGenerator.GenInputJava(table);
                        string modelExtFileName = string.Format(@"{0}\{1}ModelExt.java", txtOutDir.Text.Trim(), PostgreSqlJavaGenerator.GenJavaName(table.Name));
                        File.WriteAllText(modelExtFileName, modelExtJava);
                    } catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                }

                MessageBox.Show("Model ext files generated");
            }
        }

        private void btnGenServlet_Click(object sender, EventArgs e) {
            if (txtXml.Text.Trim() != string.Empty) {
                string[] fileList = txtXml.Text.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string file in fileList) {
                    PostgreSqlTable table = new PostgreSqlTable(file);

                    try {
                        string servletJava = PostgreSqlJavaGenerator.GenServletJava(table);
                        string servletFileName = string.Format(@"{0}\{1}Servlet.java", txtOutDir.Text.Trim(), PostgreSqlJavaGenerator.GenJavaName(table.Name));
                        File.WriteAllText(servletFileName, servletJava);
                    } catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                }

                MessageBox.Show("Servlets generated");
            }
        }

        private void btnGenWS_Click(object sender, EventArgs e) {
            if (txtXml.Text.Trim() != string.Empty) {
                string[] fileList = txtXml.Text.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string file in fileList) {
                    PostgreSqlTable table = new PostgreSqlTable(file);

                    try {
                        string webServiceJava = PostgreSqlJavaGenerator.GenWebServiceJava(table);
                        string webServiceFileName = string.Format(@"{0}\{1}WS.java", txtOutDir.Text.Trim(), PostgreSqlJavaGenerator.GenJavaName(table.Name));
                        File.WriteAllText(webServiceFileName, webServiceJava);
                    } catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                }

                MessageBox.Show("Web services generated");
            }
        }

        private void btnGenJSPNew_Click(object sender, EventArgs e) {
            if (txtXml.Text.Trim() != string.Empty) {
                string[] fileList = txtXml.Text.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string file in fileList) {
                    PostgreSqlTable table = new PostgreSqlTable(file);

                    try {
                        string newJSP = PostgresqlJspGenerator.GenNewJSP(table);
                        string newJSPFileName = string.Format(@"{0}\{1}New.jsp", txtOutDir.Text.Trim(), table.JavaNameLowerCase);
                        File.WriteAllText(newJSPFileName, newJSP);
                    } catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                }

                MessageBox.Show("JSP new form generated");
            }
        }

        private void btnGenJSPEdit_Click(object sender, EventArgs e) {
            if (txtXml.Text.Trim() != string.Empty) {
                string[] fileList = txtXml.Text.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string file in fileList) {
                    PostgreSqlTable table = new PostgreSqlTable(file);

                    try {
                        string editJSP = PostgresqlJspGenerator.GenEditJSP(table);
                        string editJSPFileName = string.Format(@"{0}\{1}Edit.jsp", txtOutDir.Text.Trim(), table.JavaNameLowerCase);
                        File.WriteAllText(editJSPFileName, editJSP);
                    } catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                }

                MessageBox.Show("JSP edit form generated");
            }
        }

        private void btnGenJSPInput_Click(object sender, EventArgs e) {
            if (txtXml.Text.Trim() != string.Empty) {
                string[] fileList = txtXml.Text.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string file in fileList) {
                    PostgreSqlTable table = new PostgreSqlTable(file);

                    try {
                        string inputJSP = PostgresqlJspGenerator.GenInputJSP(table);
                        string inputJSPFileName = string.Format(@"{0}\{1}Input.jsp", txtOutDir.Text.Trim(), table.JavaNameLowerCase);
                        File.WriteAllText(inputJSPFileName, inputJSP);
                    } catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                }

                MessageBox.Show("JSP input form generated");
            }
        }

        private void btnJSPView_Click(object sender, EventArgs e) {
            if (txtXml.Text.Trim() != string.Empty) {
                string[] fileList = txtXml.Text.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string file in fileList) {
                    PostgreSqlTable table = new PostgreSqlTable(file);

                    try {
                        string viewJSP = PostgresqlJspGenerator.GenViewJSP(table);
                        string viewJSPFileName = string.Format(@"{0}\{1}View.jsp", txtOutDir.Text.Trim(), table.JavaNameLowerCase);
                        File.WriteAllText(viewJSPFileName, viewJSP);
                    } catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                }

                MessageBox.Show("JSP view form generated");
            }
        }

        private void btnJSPList_Click(object sender, EventArgs e) {
            if (txtXml.Text.Trim() != string.Empty) {
                string[] fileList = txtXml.Text.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string file in fileList) {
                    PostgreSqlTable table = new PostgreSqlTable(file);

                    try {
                        string listJSP = PostgresqlJspGenerator.GenListJSP(table);
                        string listJSPFileName = string.Format(@"{0}\{1}List.jsp", txtOutDir.Text.Trim(), table.JavaNameLowerCase);
                        File.WriteAllText(listJSPFileName, listJSP);

                    } catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                }

                MessageBox.Show("JSP list form generated");
            }
        }

    }
}
